package com.studentmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ni.learning.exam.controller.AnswerSheetController;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.User;
import com.our.learning.infrastructure.pojo.Response;
import com.studentmanagement.dto.StudentDTO;
import com.studentmanagement.mapper.StudentMapper;
import com.studentmanagement.mapper.UserMapper;
import com.studentmanagement.po.StudentPO;
import com.studentmanagement.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hexujie
 * @date 2023-06-21 15:41
 */
@Service
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;
    private final UserMapper userMapper;
    private final AnswerSheetController answerSheetController;

    public StudentServiceImpl(StudentMapper studentMapper, UserMapper userMapper, AnswerSheetController answerSheetController) {
        this.studentMapper = studentMapper;
        this.userMapper = userMapper;
        this.answerSheetController = answerSheetController;
    }

    @Transactional
    @Override
    public Response createStudent(StudentDTO studentDTO) {

        User user = new User();
        user.setAccount(studentDTO.getAccount());
        user.setPassword(studentDTO.getPassword());
        user.setType(studentDTO.getType());

        if (userMapper.insert(user) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR);
        }
        StudentPO studentPO = studentDTO.getStudentPO();
        studentPO.setStudentId(user.getId());
        //持久化学生数据
        if (studentMapper.insert(studentPO) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR)
                    .addData("student", studentPO);
        }
        return new Response(ResponseStatusEnum.OK)
                .setMsg("创建成功")
                .addData("student", studentPO);
    }

    @Override
    public Response deleteStudent(Integer id) {
        int res1 = studentMapper.deleteById(id);
        int res2 = userMapper.deleteById(id);

        if (res1 != 1 && res2 != 1)
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("用户不存在");

        return new Response(ResponseStatusEnum.OK);
    }

    @Override
    public Response studentList(Integer stuId, Integer classId) {

        QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<>();
        if (stuId != null) {
            // 查询对应学生
            queryWrapper.eq("student_id", stuId);
        } else if (classId != null) {
            // 查询对应班级的全部学生
            queryWrapper.eq("class_id", classId);
        }
        // 当stuId和classId为空时查询所有学生的数据
        List<StudentPO> studentList = studentMapper.selectList(queryWrapper);

        return new Response(ResponseStatusEnum.OK)
                .addData("studentList", studentList);
    }

    @Override
    public Response getLearningSituation(Integer stuId) {
        List<Integer> list = answerSheetController.getIncorrectIds(stuId, null);
        return new Response(ResponseStatusEnum.OK)
                .addData("paperlist",list);
    }

    @Override
    public Response modifyStudent(StudentDTO studentDTO) {
        StudentPO studentPO = studentDTO.getStudentPO();
        if (studentMapper.selectById(studentPO.getStudentId()) == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("学生不存在");
        }
        if (studentMapper.updateById(studentPO) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR);
        }

        return new Response(ResponseStatusEnum.OK)
                .addData("student", studentPO);
    }

    @Override
    public List<StudentPO> studentListLocal(Integer stuId, Integer classId) {
        QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<>();
        if (stuId != null) {
            // 查询对应学生
            queryWrapper.eq("student_id", stuId);
        } else if (classId != null) {
            // 查询对应班级的全部学生
            queryWrapper.eq("class_id", classId);
        }
        // 当stuId和classId为空时查询所有学生的数据
        List<StudentPO> studentList = studentMapper.selectList(queryWrapper);

        return studentList;
    }
}
